<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12 0012
 * Time: 16:24
 */
defined("IN_IA")or exit("Access Denied");
global $_W,$_GPC;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"index";
$uid=$_GPC['uid'];
if($op=='index'){
    $request=array();
    //获取首页轮播图信息
    $slideData=pdo_getall('cqkundian_farm_slide',array('status'=>1,'uniacid'=>$uniacid,'slide_type'=>1),'','','rank asc');
    $request['slideData']=$slideData;

    //获取首页分类
    $typeData=pdo_getall('cqkundian_farm_home_type',array('status'=>1,'uniacid'=>$uniacid),'','','rank asc');
    $request['typeData']=$typeData;

    //查询用户是否存在
    $btm_con=array(
        'ikey'=>array('plate_one','plate_two','plate_three'),
        'uniacid'=>$uniacid,
    );
    //底部导航
    $btmData=pdo_getall('cqkundian_farm_set',$btm_con);
    if($btmData){
        $list=array();
        foreach ($btmData as $key=>$v){
            $list[$v['ikey']]=unserialize($v['value']);
        }
        if($list['plate_one']['icon']==''){
            $list['plate_one']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-01.png';
        }
        if($list['plate_two']['icon']==''){
            $list['plate_two']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-02.png';
        }
        if($list['plate_three']['icon']==''){
            $list['plate_three']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-04.png';
        }
        $request['homeBtm']=$list;
    }else{
        $list['plate_one']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-01.png';
        $list['plate_two']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-02.png';
        $list['plate_three']['icon']=$_W['siteroot'].'addons/kundian_farm/resource/image/icon-04.png';
        $request['homeBtm']=$list;
    }

    //查询我养殖的家畜是否成熟
    $animalMine=pdo_get('cqkundian_farm_animal_adopt',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>4));
    $request['animalMine']=$animalMine;
    $seedMine=pdo_get('cqkundian_farm_send_mine',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>2));
    $request['seedMine']=$seedMine;

    //关于我们的信息
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $request['aboutData']=$aboutData;
    //天气信息
    $wxSetData=pdo_get('cqkundian_farm_wx_set',array('uniacid'=>$uniacid),array('longitude','latitude','is_open_weather','appcode'));
    if($wxSetData['is_open_weather']==1){
        if(cache_load('kundian_farm_weather'.$uniacid)){
            //如果当前时间大于缓存时间,重新获取数据进行缓存
            if(cache_load('kundian_farm_weather_time'.$uniacid)+1800<time()){
                $weather=getWeather($wxSetData['appcode'],$wxSetData['longitude'].','.$wxSetData['latitude']);
                cache_write('kundian_farm_weather'.$uniacid,$weather);
                cache_write('kundian_farm_weather_time'.$uniacid,time());
            }else {
                //直接读取缓存数据
                $weather = cache_load('kundian_farm_weather'.$uniacid);
                if($weather['city']==null){
                    $weather=getWeather($wxSetData['appcode'],$wxSetData['longitude'].','.$wxSetData['latitude']);
                    cache_write('kundian_farm_weather'.$uniacid,$weather);
                    cache_write('kundian_farm_weather_time'.$uniacid,time());
                }
            }
        }else{
            $weather=getWeather($wxSetData['appcode'],$wxSetData['longitude'].','.$wxSetData['latitude']);
            cache_write('kundian_farm_weather'.$uniacid,$weather);
            cache_write('kundian_farm_weather_time'.$uniacid,time());
        }
        $request['weather']=$weather;
    }
    $request['is_open_weather']=$wxSetData['is_open_weather'];
    $request['weatherSet']=$wxSetData;
    //加载配置信息
    $setData=pdo_getall('cqkundian_farm_set',array('uniacid'=>$uniacid));
    $setDataList=array();
    foreach ($setData as $key => $value) {
        $setDataList[$value['ikey']]=$value['value'];
    }
    $request['setData']=$setDataList;

    //查询优惠券
    if($setDataList['is_open_coupon']==1){
        if($setDataList['coupon_count'] || $setDataList['coupon_count']!=0){
            $pageSize=$setDataList['coupon_count'];
        }else{
            $pageSize=4;
        }
        $couponData=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'is_delete'=>0),'','','rank asc',array(0,$pageSize));
        if(!empty($couponData)) {
            if ($uid != 0) {
                //判断用户是否已经领取了优惠券
                for ($j = 0; $j < count($couponData); $j++) {
                    $userCoupon = pdo_get('cqkundian_farm_user_coupon', array('uniacid' => $uniacid, 'uid' => $uid,'cid'=>$couponData[$j]['id']));
                    if(!empty($userCoupon)){
                        $couponData[$j]['is_get']=1;
                    }else{
                        $couponData[$j]['is_get']=2;
                    }
                }
                $request['couponData'] = array_sort($couponData, 'is_get', SORT_DESC);
            } else {
                $request['couponData'] = $couponData;
            }
        }else{
            $request['couponData']=array();
        }
    }
    //资讯
    $articleData=pdo_getall('cqkundian_farm_article',array('uniacid'=>$uniacid),'','','rank asc',array(0,3));
    $request['articleData']=$articleData;
    echo json_encode($request);die;
}

if($op=='login'){
	$uniacid=$_GPC['uniacid'];
    $userData=pdo_get('cqkundian_farm_user',array('uid'=>$_GPC['uid'],'uniacid'=>$uniacid));
    //查询我养殖的家畜是否成熟
    $animalMine=pdo_get('cqkundian_farm_animal_adopt',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>4));
    $seedMine=pdo_get('cqkundian_farm_send_mine',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>2));
    if(empty($userData)){
        $data=array(
            'uid'=>$_GPC['uid'],
            'nickname'=>$_GPC['nickname'],
            'avatarurl'=>$_GPC['avatar'],
            'uniacid'=>$uniacid,
            'create_time'=>time(),
            'openid'=>$_W['openid'],
        );
        $res=pdo_insert("cqkundian_farm_user",$data);
        if($res){
            echo json_encode(array('code'=>1,'animalMine'=>$animalMine,'seedMine'=>$seedMine));
        }else{
            echo json_encode(array('code'=>2));
        }
    }else{
        if($_GPC['avatar']!=undefined && $_GPC['avatar']!=''){
            $data=array(
                'uid'=>$_GPC['uid'],
                'nickname'=>$_GPC['nickname'],
                'avatarurl'=>$_GPC['avatar'],
                'uniacid'=>$uniacid,
                'openid'=>$_W['openid'],
            );
            if(!empty($_W['openid'])){
                $data['openid']=$_W['openid'];
            }
            pdo_update('cqkundian_farm_user',$data,array('uid'=>$uid,'uniacid'=>$uniacid));
        }
        echo json_encode(array('code'=>1,'animalMine'=>$animalMine,'seedMine'=>$seedMine));
    }
}
if($op=='getBackImg'){

    echo json_encode(array('back_img'=>$back_img));die;
}

//获取用户信息
if($op=='getUserInfo'){
    $request=array();
    $userInfo=pdo_get('cqkundian_farm_user',array('uid'=>$uid,'uniacid'=>$uniacid));
    $request['userInfo']=$userInfo;
    //查询订单信息
    if($uid!=0) {
        $noPayOrder = pdo_getall('cqkundian_farm_shop_order', array('status' => 0, 'uniacid' => $uniacid, 'uid' => $uid, 'use_is_delete' => 0));
        $request['noPayCount'] = count($noPayOrder);
        $peiOrder = pdo_getall('cqkundian_farm_shop_order', array('status' => 1, 'uniacid' => $uniacid, 'uid' => $uid, 'use_is_delete' => 0));
        $request['peiCount'] = count($peiOrder);
        $getOrder = pdo_getall('cqkundian_farm_shop_order', array('status' => 2, 'uniacid' => $uniacid, 'uid' => $uid, 'use_is_delete' => 0));
        $request['getCount'] = count($getOrder);
    }else{
        $request['noPayCount'] =0;
        $request['peiCount'] =0;
        $request['getCount'] =0;
    }
    //判断用户
    $is_admin=pdo_get('cqkundian_farm_vet',array('status'=>1,'uid'=>$uid));
    if(!empty($is_admin)){
        $request['is_admin']=1;
    }else{
        $request['is_admin']=2;
    }
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $request['aboutData']=$aboutData;
    $back_img=$_W['siteroot'].'addons/kundian_farm/resource/image/water-1.png';
    $request['back_img']=$back_img;
    echo json_encode($request);die;
}

//获取小程序导航信息
if($op=='getNavSet'){
    $setCon = array(
        'uniacid' => $uniacid,
        'ikey' => array('front_color', 'background_color'),
    );
    $setData = pdo_getall('cqkundian_farm_set', $setCon);
    $setDataList = array();
    foreach ($setData as $key => $value) {
        $setDataList[$value['ikey']] = $value['value'];
    }
    $request['setData'] = $setDataList;
    echo json_encode($request);die;
}

//获取关于我们的信息
if($op=='getAboutData'){
    $about=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    echo json_encode(array('aboutData'=>$about));die;
}
//排序
function array_sort($data,$col,$type=SORT_DESC){
//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序
    if(is_array($data)){
        $i=0;
        foreach($data as $k=>$v){
            if(key_exists($col,$v)){
                $arr[$i] = $v[$col];
                $i++;
            }else{
                continue;
            }
        }
    }else{
        return false;
    }
    array_multisort($arr,$type,$data);
    return $data;
}
//获取天气
function getWeather($appcode,$location){
    $host = "http://jisutqybmf.market.alicloudapi.com";
    $path = "/weather/query";
    $method = "GET";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "location=".$location;
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $result=curl_exec($curl);
    global $_W;
    $start=strpos($result,'{');
    $str=substr($result,$start);
    $weather=json_decode($str);
    $data=array();
    if($weather->status==0){
        $msg=$weather->result;
        $data=array(
            'city'=>$msg->city,
            'week'=>$msg->week,
            'date'=>$msg->date,
            'weather'=>$msg->weather,
            'temp'=>$msg->temp,
            'temphigh'=>$msg->temphigh,
            'templow'=>$msg->templow,
            'img'=>$_W['siteroot'].'addons/kundian_farm/resource/weather/'.$msg->img.'.png',
            'humidity'=>$msg->humidity,
            'windpower'=>$msg->windpower,
            'quality'=>$msg->aqi->quality,
        );
        $daily=$msg->daily;
        $daily_arr=array();
        for ($i=0;$i<count($daily);$i++){
            $daily_arr[$i]['date']=$daily[$i]->date;
            $daily_arr[$i]['week']=$daily[$i]->week;
            $daily_arr[$i]['weather']=$daily[$i]->day->weather;
            $daily_arr[$i]['img']=$_W['siteroot'].'addons/kundian_farm/resource/weather/'.$daily[$i]->day->img.'.png';
            $daily_arr[$i]['temphigh']=$daily[$i]->day->temphigh;
            $daily_arr[$i]['templow']=$daily[$i]->night->templow;
            $daily_arr[$i]['windpower']=$daily[$i]->day->windpower;
        }
        $data['daily']=$daily_arr;

        $hourly=$msg->hourly;
        $hourly_arr=array();
        for ($i=0;$i<count($hourly);$i++){
            $hourly_arr[$i]['time']=$hourly[$i]->time;
            $hourly_arr[$i]['weather']=$hourly[$i]->weather;
            $hourly_arr[$i]['temp']=$hourly[$i]->temp;
            $hourly_arr[$i]['img']=$_W['siteroot'].'addons/kundian_farm/resource/weather/'.$hourly[$i]->img.'.png';
        }
        $data['hourly']=$hourly_arr;
    }
    return $data;
}

